package com.qf.servlet;

import com.qf.pojo.DtsCoupon;
import com.qf.service.DtsCouponService;
import com.qf.service.DtsCouponUserService;
import com.qf.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author Ypl
 * @version V1.0
 * @Project September-mavens
 * @Package com.qf.servlet
 * @Description:
 * @Date 2022/10/21 20:06
 */
@SuppressWarnings("all")
@RestController
@RequestMapping("/admin/coupon")
public class DtsCouponController {
    @Autowired
    DtsCouponService dtsCouponService;
    @Autowired
    DtsCouponUserService dtsCouponUserService;

    /**
     * 优惠券列表
     *
     * 业务分析：
     * 1.在查询优惠券详情的时候优惠券的兑换码要随机生成并在保存到数据库的时候保存
     * 2.如果用户的优惠券使用了则查询该用户使用优惠券的订单编号
     * 3.如果在投放期间要删除优惠券，那么就要将所删除的优惠券，用户所领取的优惠券的状态修改为不可以使用或者已过期，
     *
     */
    @GetMapping("/list")
    public Object list(@RequestParam(value = "page",defaultValue = "1") Integer page,
                       @RequestParam(value = "limit",defaultValue = "20")Integer limit,
                       @RequestParam(value = "sort",defaultValue = "add_time")String sort,
                       @RequestParam(value = "order",defaultValue = "desc") String order,
                       String name,Integer type,Integer status ){

        Map<String,Object> data = dtsCouponService.findByPage(page,limit,order,sort,name,type,status);
        return ResponseUtil.ok(data);
    }

    /**
     * 优惠券添加
     */
    @PostMapping("/create")
    public Object insert(@RequestBody DtsCoupon dtsCoupon){
        dtsCouponService.insert(dtsCoupon);
        return ResponseUtil.ok(dtsCoupon);
    }

    /**
     * 优惠券详情
     */
    @GetMapping("/read")
    public  Object read(Integer id){
        DtsCoupon dtsCoupon =dtsCouponService.findById(id);
        return ResponseUtil.ok(dtsCoupon);
    }
    /**
     * 此优惠券使用的用户列表
     */
    @GetMapping("/listuser")
    public Object listuser(@RequestParam(value = "page",defaultValue = "1") Integer page,
                           @RequestParam(value = "limit",defaultValue = "20")Integer limit,
                           @RequestParam(value = "sort",defaultValue = "add_time")String sort,
                           @RequestParam(value = "order",defaultValue = "desc") String order,
                           Integer couponId ,Integer userId,Integer status){

        Map<String,Object> data = dtsCouponUserService.findByPageAndCouponId(page,limit,order,sort,couponId,userId,status);
        return ResponseUtil.ok(data);
    }

    /**
     * 优惠券修改
     */
    @PostMapping("/update")
    public Object update(@RequestBody DtsCoupon dtsCoupon){
        dtsCouponService.update(dtsCoupon);
        return ResponseUtil.ok();

    }

    /**
     * 优惠券删除
     */
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsCoupon dtsCoupon){
        dtsCouponService.delete(dtsCoupon);
        return ResponseUtil.ok();
    }
}
